/**
 * @Author: ls <ls-mac>
 * @Date:   2019-08-08 15:38:55
 * @Last modified by:   ls-mac
 * @Last modified time: 2019-11-05 15:55:27
 */

import { page, add, detail, positionList, edit, addOrEdit, deleteList } from 'services/enterprise';

export default {
  namespace: 'enterpriseList',
  state: {
    data: {
      list: [],
      pagination: {},
    },
    positionList: [],
    companyDetail: {},
  },

  effects: {
    *fetch({ payload, callback }, { call, put }) {
      const response = yield call(page, payload);
      yield put({
        type: 'saveList',
        payload: response,
      });
    },
    *detail({ payload, callback }, { call, put }) {
      const response = yield call(detail, payload);
      yield put({
        type: 'saveCompanyDetail',
        payload: response,
      });
      if (typeof callback === 'function') callback(response);
    },
    *edit({ payload, callback }, { call, put }) {
      const response = yield call(edit, payload);
      callback(response);
    },

    *positionList({ payload, callback }, { call, put }) {
      const response = yield call(positionList, payload);
      yield put({
        type: 'savePositionList',
        payload: response,
      });
    },
    *addCompany({ payload, callback }, { call, put }) {
      yield put({
        type: 'saveAddCompany',
      });
    },
    *addPositionList({ payload, callback }, { call, put }) {
      yield put({
        type: 'saveAddPositionList',
        payload,
      });
    },
    *editPositionList({ payload, callback }, { call, put }) {
      yield put({
        type: 'saveEditPositionList',
        payload,
      });
    },
    *saveCompany({ payload, callback }, { call, put }) {
      const response = yield call(add, payload);
      callback(response);
      // yield put({
      //   type: 'savePositionList',
      //   payload: response,
      // });
    },
    *changePositionList({ payload, callback }, { call, put }) {
      // const response = yield call(add, payload);
      yield put({
        type: 'saveChangePositionList',
        payload,
      });
    },

    *deleteList({ payload, callback }, { call, put }) {
      if (payload['id']) {
        const response = yield call(deleteList, payload);
        yield put({
          type: 'saveChangePositionList',
          payload,
        });
        if (callback) callback(response);
      } else {
        yield put({
          type: 'saveDeleteList',
          payload,
        });
      }

      // const response = yield call(deleteList, payload);
    },
  },

  reducers: {
    saveList(state, action) {
      const {
        data: { companyList, pageIndex, pageSize, recordCount },
      } = action.payload;

      return {
        ...state,
        data: {
          list: companyList,
          pagination: {
            current: pageIndex,
            total: recordCount,
            pageSize,
          },
        },
      };
    },
    savePositionList(state, action) {
      const newList = action.payload.data.map(v => {
        v.key = `o${v.id}`;
        return v;
      });
      return {
        ...state,
        positionList: newList,
      };
    },
    saveCompanyDetail(state, action) {
      const { data } = action.payload;
      const { positionList } = data;
      let newList = [];
      if (positionList) {
        newList = positionList.map(val => {
          const v = val;
          (v.key = `a${v.id}`), (v.type = 'edit');
          return v;
        });
      }
      return {
        ...state,
        companyDetail: data,
        positionList: newList,
      };
    },
    saveAddCompany(state, action) {
      return {
        ...state,
        companyDetail: { type: 'add' },
        positionList: [],
      };
    },
    saveAddPositionList(state, action) {
      return {
        ...state,
        positionList: [
          ...state.positionList,
          {
            key: `a${state.positionList.length + 1}`,
            type: 'add',
            companyId: action.payload,
          },
        ],
      };
    },
    saveEditPositionList(state, action) {
      state.positionList.map(v => {
        // if (v.key === action.payload.key) {
        //   v.type = 'edit';
        // }
        v.type = 'edit';
      });
      return {
        ...state,
        positionList: [...state.positionList],
      };
    },
    saveChangePositionList(state, action) {
      const newList = state.positionList.map(v => {
        if (v.key === action.payload.key) {
          return { ...v, ...action.payload };
        } else {
          return { ...v };
        }
      });
      return {
        ...state,
        positionList: [...newList],
      };
    },
    saveDeleteList(state, action) {
      const newList = state.positionList.filter(v => !(v.key === action.payload.key));
      return {
        ...state,
        positionList: [...newList],
      };
    },
  },
};
